<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>positioning.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_positioning_positioning.js.html">positioning.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Common positioning code.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.positioning&#39;);
<a name="line21"></a>goog.provide(&#39;goog.positioning.Corner&#39;);
<a name="line22"></a>goog.provide(&#39;goog.positioning.CornerBit&#39;);
<a name="line23"></a>goog.provide(&#39;goog.positioning.Overflow&#39;);
<a name="line24"></a>goog.provide(&#39;goog.positioning.OverflowStatus&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line27"></a>goog.require(&#39;goog.dom&#39;);
<a name="line28"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line29"></a>goog.require(&#39;goog.math.Box&#39;);
<a name="line30"></a>goog.require(&#39;goog.math.Coordinate&#39;);
<a name="line31"></a>goog.require(&#39;goog.math.Size&#39;);
<a name="line32"></a>goog.require(&#39;goog.style&#39;);
<a name="line33"></a>goog.require(&#39;goog.style.bidi&#39;);
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>/**
<a name="line37"></a> * Enum for representing an element corner for positioning the popup.
<a name="line38"></a> *
<a name="line39"></a> * The START constants map to LEFT if element directionality is left
<a name="line40"></a> * to right and RIGHT if the directionality is right to left.
<a name="line41"></a> * Likewise END maps to RIGHT or LEFT depending on the directionality.
<a name="line42"></a> *
<a name="line43"></a> * @enum {number}
<a name="line44"></a> */
<a name="line45"></a>goog.positioning.Corner = {
<a name="line46"></a>  TOP_LEFT: 0,
<a name="line47"></a>  TOP_RIGHT: 2,
<a name="line48"></a>  BOTTOM_LEFT: 1,
<a name="line49"></a>  BOTTOM_RIGHT: 3,
<a name="line50"></a>  TOP_START: 4,
<a name="line51"></a>  TOP_END: 6,
<a name="line52"></a>  BOTTOM_START: 5,
<a name="line53"></a>  BOTTOM_END: 7
<a name="line54"></a>};
<a name="line55"></a>
<a name="line56"></a>
<a name="line57"></a>/**
<a name="line58"></a> * Enum for bits in the {@see goog.positioning.Corner) bitmap.
<a name="line59"></a> *
<a name="line60"></a> * @enum {number}
<a name="line61"></a> */
<a name="line62"></a>goog.positioning.CornerBit = {
<a name="line63"></a>  BOTTOM: 1,
<a name="line64"></a>  RIGHT: 2,
<a name="line65"></a>  FLIP_RTL: 4
<a name="line66"></a>};
<a name="line67"></a>
<a name="line68"></a>
<a name="line69"></a>/**
<a name="line70"></a> * Enum for representing position handling in cases where the element would be
<a name="line71"></a> * positioned outside the viewport.
<a name="line72"></a> *
<a name="line73"></a> * @enum {number}
<a name="line74"></a> */
<a name="line75"></a>goog.positioning.Overflow = {
<a name="line76"></a>  /** Ignore overflow */
<a name="line77"></a>  IGNORE: 0,
<a name="line78"></a>
<a name="line79"></a>  /** Try to fit horizontally in the viewport at all costs. */
<a name="line80"></a>  ADJUST_X: 1,
<a name="line81"></a>
<a name="line82"></a>  /** If the element can&#39;t fit horizontally, report positioning failure. */
<a name="line83"></a>  FAIL_X: 2,
<a name="line84"></a>
<a name="line85"></a>  /** Try to fit vertically in the viewport at all costs. */
<a name="line86"></a>  ADJUST_Y: 4,
<a name="line87"></a>
<a name="line88"></a>  /** If the element can&#39;t fit vertically, report positioning failure. */
<a name="line89"></a>  FAIL_Y: 8,
<a name="line90"></a>
<a name="line91"></a>  /** Resize the element&#39;s width to fit in the viewport. */
<a name="line92"></a>  RESIZE_WIDTH: 16,
<a name="line93"></a>
<a name="line94"></a>  /** Resize the element&#39;s height to fit in the viewport. */
<a name="line95"></a>  RESIZE_HEIGHT: 32,
<a name="line96"></a>
<a name="line97"></a>  /**
<a name="line98"></a>   * If the anchor goes off-screen in the x-direction, position the movable
<a name="line99"></a>   * element off-screen. Otherwise, try to fit horizontally in the viewport.
<a name="line100"></a>   */
<a name="line101"></a>  ADJUST_X_EXCEPT_OFFSCREEN: 64 | 1,
<a name="line102"></a>
<a name="line103"></a>  /**
<a name="line104"></a>   * If the anchor goes off-screen in the y-direction, position the movable
<a name="line105"></a>   * element off-screen. Otherwise, try to fit vertically in the viewport.
<a name="line106"></a>   */
<a name="line107"></a>  ADJUST_Y_EXCEPT_OFFSCREEN: 128 | 4
<a name="line108"></a>};
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * Enum for representing the outcome of a positioning call.
<a name="line113"></a> *
<a name="line114"></a> * @enum {number}
<a name="line115"></a> */
<a name="line116"></a>goog.positioning.OverflowStatus = {
<a name="line117"></a>  NONE: 0,
<a name="line118"></a>  ADJUSTED_X: 1,
<a name="line119"></a>  ADJUSTED_Y: 2,
<a name="line120"></a>  WIDTH_ADJUSTED: 4,
<a name="line121"></a>  HEIGHT_ADJUSTED: 8,
<a name="line122"></a>  FAILED_LEFT: 16,
<a name="line123"></a>  FAILED_RIGHT: 32,
<a name="line124"></a>  FAILED_TOP: 64,
<a name="line125"></a>  FAILED_BOTTOM: 128,
<a name="line126"></a>  FAILED_OUTSIDE_VIEWPORT: 256
<a name="line127"></a>};
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/**
<a name="line131"></a> * Shorthand to check if a status code contains any fail code.
<a name="line132"></a> * @type {number}
<a name="line133"></a> */
<a name="line134"></a>goog.positioning.OverflowStatus.FAILED =
<a name="line135"></a>    goog.positioning.OverflowStatus.FAILED_LEFT |
<a name="line136"></a>    goog.positioning.OverflowStatus.FAILED_RIGHT |
<a name="line137"></a>    goog.positioning.OverflowStatus.FAILED_TOP |
<a name="line138"></a>    goog.positioning.OverflowStatus.FAILED_BOTTOM |
<a name="line139"></a>    goog.positioning.OverflowStatus.FAILED_OUTSIDE_VIEWPORT;
<a name="line140"></a>
<a name="line141"></a>
<a name="line142"></a>/**
<a name="line143"></a> * Shorthand to check if horizontal positioning failed.
<a name="line144"></a> * @type {number}
<a name="line145"></a> */
<a name="line146"></a>goog.positioning.OverflowStatus.FAILED_HORIZONTAL =
<a name="line147"></a>    goog.positioning.OverflowStatus.FAILED_LEFT |
<a name="line148"></a>    goog.positioning.OverflowStatus.FAILED_RIGHT;
<a name="line149"></a>
<a name="line150"></a>
<a name="line151"></a>/**
<a name="line152"></a> * Shorthand to check if vertical positioning failed.
<a name="line153"></a> * @type {number}
<a name="line154"></a> */
<a name="line155"></a>goog.positioning.OverflowStatus.FAILED_VERTICAL =
<a name="line156"></a>    goog.positioning.OverflowStatus.FAILED_TOP |
<a name="line157"></a>    goog.positioning.OverflowStatus.FAILED_BOTTOM;
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>/**
<a name="line161"></a> * Positions a movable element relative to an anchor element. The caller
<a name="line162"></a> * specifies the corners that should touch. This functions then moves the
<a name="line163"></a> * movable element accordingly.
<a name="line164"></a> *
<a name="line165"></a> * @param {Element} anchorElement The element that is the anchor for where
<a name="line166"></a> *    the movable element should position itself.
<a name="line167"></a> * @param {goog.positioning.Corner} anchorElementCorner The corner of the
<a name="line168"></a> *     anchorElement for positioning the movable element.
<a name="line169"></a> * @param {Element} movableElement The element to move.
<a name="line170"></a> * @param {goog.positioning.Corner} movableElementCorner The corner of the
<a name="line171"></a> *     movableElement that that should be positioned adjacent to the anchor
<a name="line172"></a> *     element.
<a name="line173"></a> * @param {goog.math.Coordinate=} opt_offset An offset specified in pixels.
<a name="line174"></a> *    After the normal positioning algorithm is applied, the offset is then
<a name="line175"></a> *    applied. Positive coordinates move the popup closer to the center of the
<a name="line176"></a> *    anchor element. Negative coordinates move the popup away from the center
<a name="line177"></a> *    of the anchor element.
<a name="line178"></a> * @param {goog.math.Box=} opt_margin A margin specified in pixels.
<a name="line179"></a> *    After the normal positioning algorithm is applied and any offset, the
<a name="line180"></a> *    margin is then applied. Positive coordinates move the popup away from the
<a name="line181"></a> *    spot it was positioned towards its center. Negative coordinates move it
<a name="line182"></a> *    towards the spot it was positioned away from its center.
<a name="line183"></a> * @param {?number=} opt_overflow Overflow handling mode. Defaults to IGNORE if
<a name="line184"></a> *     not specified. Bitmap, {@see goog.positioning.Overflow}.
<a name="line185"></a> * @param {goog.math.Size=} opt_preferredSize The preferred size of the
<a name="line186"></a> *     movableElement.
<a name="line187"></a> * @param {goog.math.Box=} opt_viewport Box object describing the dimensions of
<a name="line188"></a> *     the viewport. If not provided, a default one will be calculated by the
<a name="line189"></a> *     position of the anchorElement and its moveable parent element.
<a name="line190"></a> * @return {goog.positioning.OverflowStatus} Status bitmap,
<a name="line191"></a> *     {@see goog.positioning.OverflowStatus}.
<a name="line192"></a> */
<a name="line193"></a>goog.positioning.positionAtAnchor = function(anchorElement,
<a name="line194"></a>                                             anchorElementCorner,
<a name="line195"></a>                                             movableElement,
<a name="line196"></a>                                             movableElementCorner,
<a name="line197"></a>                                             opt_offset,
<a name="line198"></a>                                             opt_margin,
<a name="line199"></a>                                             opt_overflow,
<a name="line200"></a>                                             opt_preferredSize,
<a name="line201"></a>                                             opt_viewport) {
<a name="line202"></a>
<a name="line203"></a>  goog.asserts.assert(movableElement);
<a name="line204"></a>  var movableParentTopLeft =
<a name="line205"></a>      goog.positioning.getOffsetParentPageOffset(movableElement);
<a name="line206"></a>
<a name="line207"></a>  // Get the visible part of the anchor element.  anchorRect is
<a name="line208"></a>  // relative to anchorElement&#39;s page.
<a name="line209"></a>  var anchorRect = goog.positioning.getVisiblePart_(anchorElement);
<a name="line210"></a>
<a name="line211"></a>  // Translate anchorRect to be relative to movableElement&#39;s page.
<a name="line212"></a>  goog.style.translateRectForAnotherFrame(
<a name="line213"></a>      anchorRect,
<a name="line214"></a>      goog.dom.getDomHelper(anchorElement),
<a name="line215"></a>      goog.dom.getDomHelper(movableElement));
<a name="line216"></a>
<a name="line217"></a>  // Offset based on which corner of the element we want to position against.
<a name="line218"></a>  var corner = goog.positioning.getEffectiveCorner(anchorElement,
<a name="line219"></a>                                                   anchorElementCorner);
<a name="line220"></a>  // absolutePos is a candidate position relative to the
<a name="line221"></a>  // movableElement&#39;s window.
<a name="line222"></a>  var absolutePos = new goog.math.Coordinate(
<a name="line223"></a>      corner &amp; goog.positioning.CornerBit.RIGHT ?
<a name="line224"></a>          anchorRect.left + anchorRect.width : anchorRect.left,
<a name="line225"></a>      corner &amp; goog.positioning.CornerBit.BOTTOM ?
<a name="line226"></a>          anchorRect.top + anchorRect.height : anchorRect.top);
<a name="line227"></a>
<a name="line228"></a>  // Translate absolutePos to be relative to the offsetParent.
<a name="line229"></a>  absolutePos =
<a name="line230"></a>      goog.math.Coordinate.difference(absolutePos, movableParentTopLeft);
<a name="line231"></a>
<a name="line232"></a>  // Apply offset, if specified
<a name="line233"></a>  if (opt_offset) {
<a name="line234"></a>    absolutePos.x += (corner &amp; goog.positioning.CornerBit.RIGHT ? -1 : 1) *
<a name="line235"></a>        opt_offset.x;
<a name="line236"></a>    absolutePos.y += (corner &amp; goog.positioning.CornerBit.BOTTOM ? -1 : 1) *
<a name="line237"></a>        opt_offset.y;
<a name="line238"></a>  }
<a name="line239"></a>
<a name="line240"></a>  // Determine dimension of viewport.
<a name="line241"></a>  var viewport;
<a name="line242"></a>  if (opt_overflow) {
<a name="line243"></a>    if (opt_viewport) {
<a name="line244"></a>      viewport = opt_viewport;
<a name="line245"></a>    } else {
<a name="line246"></a>      viewport = goog.style.getVisibleRectForElement(movableElement);
<a name="line247"></a>      if (viewport) {
<a name="line248"></a>        viewport.top -= movableParentTopLeft.y;
<a name="line249"></a>        viewport.right -= movableParentTopLeft.x;
<a name="line250"></a>        viewport.bottom -= movableParentTopLeft.y;
<a name="line251"></a>        viewport.left -= movableParentTopLeft.x;
<a name="line252"></a>      }
<a name="line253"></a>    }
<a name="line254"></a>  }
<a name="line255"></a>
<a name="line256"></a>  return goog.positioning.positionAtCoordinate(absolutePos,
<a name="line257"></a>                                               movableElement,
<a name="line258"></a>                                               movableElementCorner,
<a name="line259"></a>                                               opt_margin,
<a name="line260"></a>                                               viewport,
<a name="line261"></a>                                               opt_overflow,
<a name="line262"></a>                                               opt_preferredSize);
<a name="line263"></a>};
<a name="line264"></a>
<a name="line265"></a>
<a name="line266"></a>/**
<a name="line267"></a> * Calculates the page offset of the given element&#39;s
<a name="line268"></a> * offsetParent. This value can be used to translate any x- and
<a name="line269"></a> * y-offset relative to the page to an offset relative to the
<a name="line270"></a> * offsetParent, which can then be used directly with as position
<a name="line271"></a> * coordinate for {@code positionWithCoordinate}.
<a name="line272"></a> * @param {!Element} movableElement The element to calculate.
<a name="line273"></a> * @return {!goog.math.Coordinate} The page offset, may be (0, 0).
<a name="line274"></a> */
<a name="line275"></a>goog.positioning.getOffsetParentPageOffset = function(movableElement) {
<a name="line276"></a>  // Ignore offset for the BODY element unless its position is non-static.
<a name="line277"></a>  // For cases where the offset parent is HTML rather than the BODY (such as in
<a name="line278"></a>  // IE strict mode) there&#39;s no need to get the position of the BODY as it
<a name="line279"></a>  // doesn&#39;t affect the page offset.
<a name="line280"></a>  var movableParentTopLeft;
<a name="line281"></a>  var parent = movableElement.offsetParent;
<a name="line282"></a>  if (parent) {
<a name="line283"></a>    var isBody = parent.tagName == goog.dom.TagName.HTML ||
<a name="line284"></a>        parent.tagName == goog.dom.TagName.BODY;
<a name="line285"></a>    if (!isBody ||
<a name="line286"></a>        goog.style.getComputedPosition(parent) != &#39;static&#39;) {
<a name="line287"></a>      // Get the top-left corner of the parent, in page coordinates.
<a name="line288"></a>      movableParentTopLeft = goog.style.getPageOffset(parent);
<a name="line289"></a>
<a name="line290"></a>      if (!isBody) {
<a name="line291"></a>        movableParentTopLeft = goog.math.Coordinate.difference(
<a name="line292"></a>            movableParentTopLeft,
<a name="line293"></a>            new goog.math.Coordinate(goog.style.bidi.getScrollLeft(parent),
<a name="line294"></a>                parent.scrollTop));
<a name="line295"></a>      }
<a name="line296"></a>    }
<a name="line297"></a>  }
<a name="line298"></a>
<a name="line299"></a>  return movableParentTopLeft || new goog.math.Coordinate();
<a name="line300"></a>};
<a name="line301"></a>
<a name="line302"></a>
<a name="line303"></a>/**
<a name="line304"></a> * Returns intersection of the specified element and
<a name="line305"></a> * goog.style.getVisibleRectForElement for it.
<a name="line306"></a> *
<a name="line307"></a> * @param {Element} el The target element.
<a name="line308"></a> * @return {goog.math.Rect} Intersection of getVisibleRectForElement
<a name="line309"></a> *     and the current bounding rectangle of the element.  If the
<a name="line310"></a> *     intersection is empty, returns the bounding rectangle.
<a name="line311"></a> * @private
<a name="line312"></a> */
<a name="line313"></a>goog.positioning.getVisiblePart_ = function(el) {
<a name="line314"></a>  var rect = goog.style.getBounds(el);
<a name="line315"></a>  var visibleBox = goog.style.getVisibleRectForElement(el);
<a name="line316"></a>  if (visibleBox) {
<a name="line317"></a>    rect.intersection(goog.math.Rect.createFromBox(visibleBox));
<a name="line318"></a>  }
<a name="line319"></a>  return rect;
<a name="line320"></a>};
<a name="line321"></a>
<a name="line322"></a>
<a name="line323"></a>/**
<a name="line324"></a> * Positions the specified corner of the movable element at the
<a name="line325"></a> * specified coordinate.
<a name="line326"></a> *
<a name="line327"></a> * @param {goog.math.Coordinate} absolutePos The coordinate to position the
<a name="line328"></a> *     element at.
<a name="line329"></a> * @param {Element} movableElement The element to be positioned.
<a name="line330"></a> * @param {goog.positioning.Corner} movableElementCorner The corner of the
<a name="line331"></a> *     movableElement that that should be positioned.
<a name="line332"></a> * @param {goog.math.Box=} opt_margin A margin specified in pixels.
<a name="line333"></a> *    After the normal positioning algorithm is applied and any offset, the
<a name="line334"></a> *    margin is then applied. Positive coordinates move the popup away from the
<a name="line335"></a> *    spot it was positioned towards its center. Negative coordinates move it
<a name="line336"></a> *    towards the spot it was positioned away from its center.
<a name="line337"></a> * @param {goog.math.Box=} opt_viewport Box object describing the dimensions of
<a name="line338"></a> *     the viewport. Required if opt_overflow is specified.
<a name="line339"></a> * @param {?number=} opt_overflow Overflow handling mode. Defaults to IGNORE if
<a name="line340"></a> *     not specified, {@see goog.positioning.Overflow}.
<a name="line341"></a> * @param {goog.math.Size=} opt_preferredSize The preferred size of the
<a name="line342"></a> *     movableElement. Defaults to the current size.
<a name="line343"></a> * @return {goog.positioning.OverflowStatus} Status bitmap.
<a name="line344"></a> */
<a name="line345"></a>goog.positioning.positionAtCoordinate = function(absolutePos,
<a name="line346"></a>                                                 movableElement,
<a name="line347"></a>                                                 movableElementCorner,
<a name="line348"></a>                                                 opt_margin,
<a name="line349"></a>                                                 opt_viewport,
<a name="line350"></a>                                                 opt_overflow,
<a name="line351"></a>                                                 opt_preferredSize) {
<a name="line352"></a>  absolutePos = absolutePos.clone();
<a name="line353"></a>  var status = goog.positioning.OverflowStatus.NONE;
<a name="line354"></a>
<a name="line355"></a>  // Offset based on attached corner and desired margin.
<a name="line356"></a>  var corner = goog.positioning.getEffectiveCorner(movableElement,
<a name="line357"></a>                                                   movableElementCorner);
<a name="line358"></a>  var elementSize = goog.style.getSize(movableElement);
<a name="line359"></a>  var size = opt_preferredSize ? opt_preferredSize.clone() :
<a name="line360"></a>      elementSize.clone();
<a name="line361"></a>
<a name="line362"></a>  if (opt_margin || corner != goog.positioning.Corner.TOP_LEFT) {
<a name="line363"></a>    if (corner &amp; goog.positioning.CornerBit.RIGHT) {
<a name="line364"></a>      absolutePos.x -= size.width + (opt_margin ? opt_margin.right : 0);
<a name="line365"></a>    } else if (opt_margin) {
<a name="line366"></a>      absolutePos.x += opt_margin.left;
<a name="line367"></a>    }
<a name="line368"></a>    if (corner &amp; goog.positioning.CornerBit.BOTTOM) {
<a name="line369"></a>      absolutePos.y -= size.height + (opt_margin ? opt_margin.bottom : 0);
<a name="line370"></a>    } else if (opt_margin) {
<a name="line371"></a>      absolutePos.y += opt_margin.top;
<a name="line372"></a>    }
<a name="line373"></a>  }
<a name="line374"></a>
<a name="line375"></a>  // Adjust position to fit inside viewport.
<a name="line376"></a>  if (opt_overflow) {
<a name="line377"></a>    status = opt_viewport ?
<a name="line378"></a>        goog.positioning.adjustForViewport_(
<a name="line379"></a>            absolutePos, size, opt_viewport, opt_overflow) :
<a name="line380"></a>        goog.positioning.OverflowStatus.FAILED_OUTSIDE_VIEWPORT;
<a name="line381"></a>    if (status &amp; goog.positioning.OverflowStatus.FAILED) {
<a name="line382"></a>      return status;
<a name="line383"></a>    }
<a name="line384"></a>  }
<a name="line385"></a>
<a name="line386"></a>  goog.style.setPosition(movableElement, absolutePos);
<a name="line387"></a>  if (!goog.math.Size.equals(elementSize, size)) {
<a name="line388"></a>    goog.style.setBorderBoxSize(movableElement, size);
<a name="line389"></a>  }
<a name="line390"></a>
<a name="line391"></a>  return status;
<a name="line392"></a>};
<a name="line393"></a>
<a name="line394"></a>
<a name="line395"></a>/**
<a name="line396"></a> * Adjusts the position and/or size of an element, identified by its position
<a name="line397"></a> * and size, to fit inside the viewport. If the position or size of the element
<a name="line398"></a> * is adjusted the pos or size objects, respectively, are modified.
<a name="line399"></a> *
<a name="line400"></a> * @param {goog.math.Coordinate} pos Position of element, updated if the
<a name="line401"></a> *     position is adjusted.
<a name="line402"></a> * @param {goog.math.Size} size Size of element, updated if the size is
<a name="line403"></a> *     adjusted.
<a name="line404"></a> * @param {goog.math.Box} viewport Bounding box describing the viewport.
<a name="line405"></a> * @param {number} overflow Overflow handling mode,
<a name="line406"></a> *     {@see goog.positioning.Overflow}.
<a name="line407"></a> * @return {goog.positioning.OverflowStatus} Status bitmap,
<a name="line408"></a> *     {@see goog.positioning.OverflowStatus}.
<a name="line409"></a> * @private
<a name="line410"></a> */
<a name="line411"></a>goog.positioning.adjustForViewport_ = function(pos, size, viewport, overflow) {
<a name="line412"></a>  var status = goog.positioning.OverflowStatus.NONE;
<a name="line413"></a>
<a name="line414"></a>  var ADJUST_X_EXCEPT_OFFSCREEN =
<a name="line415"></a>      goog.positioning.Overflow.ADJUST_X_EXCEPT_OFFSCREEN;
<a name="line416"></a>  var ADJUST_Y_EXCEPT_OFFSCREEN =
<a name="line417"></a>      goog.positioning.Overflow.ADJUST_Y_EXCEPT_OFFSCREEN;
<a name="line418"></a>  if ((overflow &amp; ADJUST_X_EXCEPT_OFFSCREEN) == ADJUST_X_EXCEPT_OFFSCREEN &amp;&amp;
<a name="line419"></a>      (pos.x &lt; viewport.left || pos.x &gt;= viewport.right)) {
<a name="line420"></a>    overflow &amp;= ~goog.positioning.Overflow.ADJUST_X;
<a name="line421"></a>  }
<a name="line422"></a>  if ((overflow &amp; ADJUST_Y_EXCEPT_OFFSCREEN) == ADJUST_Y_EXCEPT_OFFSCREEN &amp;&amp;
<a name="line423"></a>      (pos.y &lt; viewport.top || pos.y &gt;= viewport.bottom)) {
<a name="line424"></a>    overflow &amp;= ~goog.positioning.Overflow.ADJUST_Y;
<a name="line425"></a>  }
<a name="line426"></a>
<a name="line427"></a>  // Left edge outside viewport, try to move it.
<a name="line428"></a>  if (pos.x &lt; viewport.left &amp;&amp; overflow &amp; goog.positioning.Overflow.ADJUST_X) {
<a name="line429"></a>    pos.x = viewport.left;
<a name="line430"></a>    status |= goog.positioning.OverflowStatus.ADJUSTED_X;
<a name="line431"></a>  }
<a name="line432"></a>
<a name="line433"></a>  // Left edge inside and right edge outside viewport, try to resize it.
<a name="line434"></a>  if (pos.x &lt; viewport.left &amp;&amp;
<a name="line435"></a>      pos.x + size.width &gt; viewport.right &amp;&amp;
<a name="line436"></a>      overflow &amp; goog.positioning.Overflow.RESIZE_WIDTH) {
<a name="line437"></a>    size.width = Math.max(
<a name="line438"></a>        size.width - ((pos.x + size.width) - viewport.right), 0);
<a name="line439"></a>    status |= goog.positioning.OverflowStatus.WIDTH_ADJUSTED;
<a name="line440"></a>  }
<a name="line441"></a>
<a name="line442"></a>  // Right edge outside viewport, try to move it.
<a name="line443"></a>  if (pos.x + size.width &gt; viewport.right &amp;&amp;
<a name="line444"></a>      overflow &amp; goog.positioning.Overflow.ADJUST_X) {
<a name="line445"></a>    pos.x = Math.max(viewport.right - size.width, viewport.left);
<a name="line446"></a>    status |= goog.positioning.OverflowStatus.ADJUSTED_X;
<a name="line447"></a>  }
<a name="line448"></a>
<a name="line449"></a>  // Left or right edge still outside viewport, fail if the FAIL_X option was
<a name="line450"></a>  // specified, ignore it otherwise.
<a name="line451"></a>  if (overflow &amp; goog.positioning.Overflow.FAIL_X) {
<a name="line452"></a>    status |= (pos.x &lt; viewport.left ?
<a name="line453"></a>                   goog.positioning.OverflowStatus.FAILED_LEFT : 0) |
<a name="line454"></a>              (pos.x + size.width &gt; viewport.right ?
<a name="line455"></a>                   goog.positioning.OverflowStatus.FAILED_RIGHT : 0);
<a name="line456"></a>  }
<a name="line457"></a>
<a name="line458"></a>  // Top edge outside viewport, try to move it.
<a name="line459"></a>  if (pos.y &lt; viewport.top &amp;&amp; overflow &amp; goog.positioning.Overflow.ADJUST_Y) {
<a name="line460"></a>    pos.y = viewport.top;
<a name="line461"></a>    status |= goog.positioning.OverflowStatus.ADJUSTED_Y;
<a name="line462"></a>  }
<a name="line463"></a>
<a name="line464"></a>  // Bottom edge inside and top edge outside viewport, try to resize it.
<a name="line465"></a>  if (pos.y &lt;= viewport.top &amp;&amp;
<a name="line466"></a>      pos.y + size.height &lt; viewport.bottom &amp;&amp;
<a name="line467"></a>      overflow &amp; goog.positioning.Overflow.RESIZE_HEIGHT) {
<a name="line468"></a>    size.height = Math.max(size.height - (viewport.top - pos.y), 0);
<a name="line469"></a>    pos.y = 0;
<a name="line470"></a>    status |= goog.positioning.OverflowStatus.HEIGHT_ADJUSTED;
<a name="line471"></a>  }
<a name="line472"></a>
<a name="line473"></a>  // Top edge inside and bottom edge outside viewport, try to resize it.
<a name="line474"></a>  if (pos.y &gt;= viewport.top &amp;&amp;
<a name="line475"></a>      pos.y + size.height &gt; viewport.bottom &amp;&amp;
<a name="line476"></a>      overflow &amp; goog.positioning.Overflow.RESIZE_HEIGHT) {
<a name="line477"></a>    size.height = Math.max(
<a name="line478"></a>        size.height - ((pos.y + size.height) - viewport.bottom), 0);
<a name="line479"></a>    status |= goog.positioning.OverflowStatus.HEIGHT_ADJUSTED;
<a name="line480"></a>  }
<a name="line481"></a>
<a name="line482"></a>  // Bottom edge outside viewport, try to move it.
<a name="line483"></a>  if (pos.y + size.height &gt; viewport.bottom &amp;&amp;
<a name="line484"></a>      overflow &amp; goog.positioning.Overflow.ADJUST_Y) {
<a name="line485"></a>    pos.y = Math.max(viewport.bottom - size.height, viewport.top);
<a name="line486"></a>    status |= goog.positioning.OverflowStatus.ADJUSTED_Y;
<a name="line487"></a>  }
<a name="line488"></a>
<a name="line489"></a>  // Top or bottom edge still outside viewport, fail if the FAIL_Y option was
<a name="line490"></a>  // specified, ignore it otherwise.
<a name="line491"></a>  if (overflow &amp; goog.positioning.Overflow.FAIL_Y) {
<a name="line492"></a>    status |= (pos.y &lt; viewport.top ?
<a name="line493"></a>                   goog.positioning.OverflowStatus.FAILED_TOP : 0) |
<a name="line494"></a>              (pos.y + size.height &gt; viewport.bottom ?
<a name="line495"></a>                   goog.positioning.OverflowStatus.FAILED_BOTTOM : 0);
<a name="line496"></a>  }
<a name="line497"></a>
<a name="line498"></a>  return status;
<a name="line499"></a>};
<a name="line500"></a>
<a name="line501"></a>
<a name="line502"></a>/**
<a name="line503"></a> * Returns an absolute corner (top/bottom left/right) given an absolute
<a name="line504"></a> * or relative (top/bottom start/end) corner and the direction of an element.
<a name="line505"></a> * Absolute corners remain unchanged.
<a name="line506"></a> * @param {Element} element DOM element to test for RTL direction.
<a name="line507"></a> * @param {goog.positioning.Corner} corner The popup corner used for
<a name="line508"></a> *     positioning.
<a name="line509"></a> * @return {goog.positioning.Corner} Effective corner.
<a name="line510"></a> */
<a name="line511"></a>goog.positioning.getEffectiveCorner = function(element, corner) {
<a name="line512"></a>  return /** @type {goog.positioning.Corner} */ (
<a name="line513"></a>      (corner &amp; goog.positioning.CornerBit.FLIP_RTL &amp;&amp;
<a name="line514"></a>          goog.style.isRightToLeft(element) ?
<a name="line515"></a>          corner ^ goog.positioning.CornerBit.RIGHT :
<a name="line516"></a>          corner
<a name="line517"></a>      ) &amp; ~goog.positioning.CornerBit.FLIP_RTL);
<a name="line518"></a>};
<a name="line519"></a>
<a name="line520"></a>
<a name="line521"></a>/**
<a name="line522"></a> * Returns the corner opposite the given one horizontally.
<a name="line523"></a> * @param {goog.positioning.Corner} corner The popup corner used to flip.
<a name="line524"></a> * @return {goog.positioning.Corner} The opposite corner horizontally.
<a name="line525"></a> */
<a name="line526"></a>goog.positioning.flipCornerHorizontal = function(corner) {
<a name="line527"></a>  return /** @type {goog.positioning.Corner} */ (corner ^
<a name="line528"></a>      goog.positioning.CornerBit.RIGHT);
<a name="line529"></a>};
<a name="line530"></a>
<a name="line531"></a>
<a name="line532"></a>/**
<a name="line533"></a> * Returns the corner opposite the given one vertically.
<a name="line534"></a> * @param {goog.positioning.Corner} corner The popup corner used to flip.
<a name="line535"></a> * @return {goog.positioning.Corner} The opposite corner vertically.
<a name="line536"></a> */
<a name="line537"></a>goog.positioning.flipCornerVertical = function(corner) {
<a name="line538"></a>  return /** @type {goog.positioning.Corner} */ (corner ^
<a name="line539"></a>      goog.positioning.CornerBit.BOTTOM);
<a name="line540"></a>};
<a name="line541"></a>
<a name="line542"></a>
<a name="line543"></a>/**
<a name="line544"></a> * Returns the corner opposite the given one horizontally and vertically.
<a name="line545"></a> * @param {goog.positioning.Corner} corner The popup corner used to flip.
<a name="line546"></a> * @return {goog.positioning.Corner} The opposite corner horizontally and
<a name="line547"></a> *     vertically.
<a name="line548"></a> */
<a name="line549"></a>goog.positioning.flipCorner = function(corner) {
<a name="line550"></a>  return /** @type {goog.positioning.Corner} */ (corner ^
<a name="line551"></a>      goog.positioning.CornerBit.BOTTOM ^
<a name="line552"></a>      goog.positioning.CornerBit.RIGHT);
<a name="line553"></a>};
</pre>


</body>
</html>
